/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelRelatorioAparelhosAcabados.java
 *
 * Created on 04/10/2011, 21:27:27
 */

package modules.enderecamento.gui.jpanel;

import java.util.List;
import modules.enderecamento.bo.RelatorioBO;
import modules.enderecamento.facade.WMSFacade;
import modules.enderecamento.models.dto.Usuario;

/**
 *
 * @author clezio
 */
public class PanelRelatorioAparelhosAcabados extends javax.swing.JPanel {

    private WMSFacade wmsFacade;
    /** Creates new form PanelRelatorioAparelhosAcabados */
    public PanelRelatorioAparelhosAcabados() {
        initComponents();
        wmsFacade = new WMSFacade();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbUsuario1 = new modules.enderecamento.gui.custom.WMSJComboBox();
        lbDescricao = new modules.enderecamento.gui.custom.WMSLabel();
        cbUsuario2 = new modules.enderecamento.gui.custom.WMSJComboBox();
        lbDescricao1 = new modules.enderecamento.gui.custom.WMSLabel();
        jLabel1 = new javax.swing.JLabel();
        btnImprimir = new modules.enderecamento.gui.custom.WMSJButton();

        setBackground(new java.awt.Color(238, 238, 238));
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        cbUsuario1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione" }));
        cbUsuario1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cbUsuario1KeyPressed(evt);
            }
        });

        lbDescricao.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbDescricao.setText("1ª assinatura:");

        cbUsuario2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione" }));
        cbUsuario2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cbUsuario2KeyPressed(evt);
            }
        });

        lbDescricao1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbDescricao1.setText("2ª assinatura:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Impressão relatório de produtos acabados");

        btnImprimir.setMnemonic('i');
        btnImprimir.setText("Imprimir");
        btnImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbUsuario1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbDescricao1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbUsuario2, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(515, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 800, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(94, 94, 94)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbUsuario1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbUsuario2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbDescricao1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(374, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cbUsuario1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbUsuario1KeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            cbUsuario2.requestFocus();
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            cbUsuario1.setSelectedIndex(0);
        }
}//GEN-LAST:event_cbUsuario1KeyPressed

    private void cbUsuario2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbUsuario2KeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            imprimir();
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            cbUsuario2.setSelectedIndex(0);
            cbUsuario1.requestFocus();
        }
    }//GEN-LAST:event_cbUsuario2KeyPressed

    private void btnImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirActionPerformed
        imprimir();
}//GEN-LAST:event_btnImprimirActionPerformed

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        preencheCbUsuarios(wmsFacade.obterUsuarios("from Usuario order by nome"));
    }//GEN-LAST:event_formAncestorAdded


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private modules.enderecamento.gui.custom.WMSJButton btnImprimir;
    private modules.enderecamento.gui.custom.WMSJComboBox cbUsuario1;
    private modules.enderecamento.gui.custom.WMSJComboBox cbUsuario2;
    private javax.swing.JLabel jLabel1;
    private modules.enderecamento.gui.custom.WMSLabel lbDescricao;
    private modules.enderecamento.gui.custom.WMSLabel lbDescricao1;
    // End of variables declaration//GEN-END:variables

    private void preencheCbUsuarios(List<Usuario> lista){
        cbUsuario1.removeAllItems();
        cbUsuario2.removeAllItems();
        cbUsuario1.addItem("Selecione");
        cbUsuario2.addItem("Selecione");
        for (int i= 0; i < lista.size(); i++) {
            cbUsuario1.addItem(lista.get(i));
            cbUsuario2.addItem(lista.get(i));
        }
    }

    private void imprimir() {
        Usuario usr1 = null;
        Usuario usr2 = null;
        if(cbUsuario1.getSelectedIndex() > 0){
            usr1 = (Usuario) cbUsuario1.getSelectedItem();
        }
        if(cbUsuario2.getSelectedIndex() > 0){
            usr2 = (Usuario) cbUsuario2.getSelectedItem();
        }
        new RelatorioBO().produtosAcabados(usr1,usr2);
        clearFields();
    }

    private void clearFields(){
        cbUsuario1.setSelectedIndex(0);
        cbUsuario2.setSelectedIndex(0);
        cbUsuario1.requestFocus();
    }
}
